package org.txazo.stat.util;

import javax.servlet.http.HttpServletRequest;

/**
 * Http工具类
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 * 
 */
public class HttpUtil {

	/**
	 * 获取完整请求URL
	 */
	public static String getRequestURLAndParameters(HttpServletRequest request) {
		String uri = request.getRequestURI();
		if (isStaticResource(uri)) {
			return null;
		}

		StringBuffer sb = request.getRequestURL();

		String queryString = request.getQueryString();
		if (StringUtil.isNotEmpty(queryString)) {
			sb.append("?");
			sb.append(queryString);
		}

		return sb.toString();
	}

	/**
	 * 是否静态资源
	 */
	public static boolean isStaticResource(String uri) {
		if (StringUtil.isNotEmpty(uri)
				&& uri.contains(ConstantsUtil.STRING_DIT)) {
			String type = uri
					.substring(uri.lastIndexOf(ConstantsUtil.STRING_DIT) + 1,
							uri.length());
			int length = ConstantsUtil.STATIC_RESOURCES_SUFFIX.length;
			for (int i = 0; i < length; i++) {
				if (StringUtil.isNotEmpty(type)
						&& type.equals(ConstantsUtil.STATIC_RESOURCES_SUFFIX[i])) {
					return true;
				}
			}
		}
		return false;
	}

}
